package com.igl.talk.android.client.core.handlers;
/*
 * @author Adam Wieczorek 
 *
 * 26/03/2014
 *
 * All rights reserved.
 */

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import com.igl.talk.android.client.R;
import com.igl.talk.android.client.core.model.MessageModel;
import com.igl.talk.android.client.core.model.UserModel;
import com.igl.talk.android.client.ide.models.IdeMessageModel;

import java.util.List;

public class ClientHandler extends Handler implements HandlerInteface {

    private static ArrayAdapter viewAdapter = null;
    private static List<MessageModel> messages = null;

    private static ClientHandler instance = null;

    private static Context applicationContext = null;

    private static String clientUsername = "sender";

    private ClientHandler() {}

    public static ClientHandler getInstance() {
        if (instance == null) {
            instance = new ClientHandler();
        }
        return instance;
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
/*                case MSG_RESPONSE:
                Toast.makeText(getApplicationContext(), "ClientExample got respond happily :)" + msg.arg1, Toast.LENGTH_SHORT).show();
                break;
            case MSG_JSON:
                Toast.makeText(getApplicationContext(), msg.obj.toString(), Toast.LENGTH_LONG).show();*/
            case MSG_NEW_USER_JOINED:
                if (applicationContext != null) {
                    Toast.makeText(applicationContext, applicationContext.getResources()
                            .getString(R.string.user_joined_chat, ((UserModel)msg.obj).getName()), Toast.LENGTH_SHORT).show();
                }
                break;
            case MSG_RECEIVED_MESSAGE:
                populateMessage((MessageModel) msg.obj);
                break;
            case MSG_USER_LEAVED:
                if (applicationContext != null) {
                    Toast.makeText(applicationContext, applicationContext.getResources()
                            .getString(R.string.user_left_chat, ((UserModel)msg.obj).getName()), Toast.LENGTH_SHORT).show();
                }
                break;
            case MSG_USER_LIST:
                //occurs when server pushes new user list, in msg.obj we have List<User>
                break;

            default:
                super.handleMessage(msg);
        }
    }

    private void populateMessage(MessageModel msg) {
        // because we propagate any message, even sent by us, we need to check username
        if (!clientUsername.equals(msg.getUser().getName())) {
            viewAdapter.add(new IdeMessageModel(msg.getUser().getName(), msg.getContent(), true));
            messages.add(msg);
        }
    }

    public ArrayAdapter<?> getViewAdapter() {
        return viewAdapter;
    }

    public void setViewAdapter(ArrayAdapter<?> viewAdapter) {
        this.viewAdapter = viewAdapter;
    }

    public List<MessageModel> getMessages() {
        return messages;
    }

    public void setMessages(List<MessageModel> messages) {
        this.messages = messages;
    }

    public static void setClientUsername(String clientUsername) {
        ClientHandler.clientUsername = clientUsername;
    }

    public static void setApplicationContext(Context applicationContext) {
        ClientHandler.applicationContext = applicationContext;
    }
}